/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdaugaComandaAferenta.java
 *
 * Created on Apr 10, 2010, 2:47:06 PM
 */

package point.of.sale.app;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author card3r
 */
public class Clienti__existenti__onClick extends javax.swing.JFrame {
    public static int id_client;
    public static int[] idPizza;
    public static int i,j;
    public static int qty[];
    public static int[] transferedId;
    public SQLHandler sqlh = new SQLHandler();

    /** Creates new form AdaugaComandaAferenta */
    public Clienti__existenti__onClick() {

        initComponents();
        initProductsList();
        qty = new int[10000];
        transferedId= new int[10000];
        j=0;
    }
    public  void initProductsList()
    {
        try {
            i=0;
            idPizza=new int[10000];

            
            ResultSet rs = sqlh.genericSelect("SELECT * FROM produs");
            while (rs.next()) {
                list1ProduseExistente.add(rs.getString("nume"));
                idPizza[i]=rs.getInt("id_produs");
                i++;
            }
        } catch (SQLException ex) {
            Logger.getLogger(Clienti__existenti__onClick.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        SpinnerCantitate = new javax.swing.JSpinner();
        list1ProduseExistente = new java.awt.List();
        TextFieldItemSelectat = new javax.swing.JTextField();
        listProduseSelectate = new java.awt.List();
        ButtonRevocare = new javax.swing.JButton();
        ButtonAdaugaInLista = new javax.swing.JButton();
        ButtonProceseaza = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        list1ProduseExistente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                list1ProduseExistenteActionPerformed(evt);
            }
        });

        ButtonRevocare.setText("Revocare");
        ButtonRevocare.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonRevocareActionPerformed(evt);
            }
        });

        ButtonAdaugaInLista.setText("<<Adauga in lista");
        ButtonAdaugaInLista.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonAdaugaInListaActionPerformed(evt);
            }
        });

        ButtonProceseaza.setText("Proceseaza");
        ButtonProceseaza.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonProceseazaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(listProduseSelectate, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(ButtonRevocare, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(ButtonProceseaza, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(ButtonAdaugaInLista, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(TextFieldItemSelectat, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(SpinnerCantitate, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(list1ProduseExistente, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(listProduseSelectate, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(list1ProduseExistente, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(SpinnerCantitate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(TextFieldItemSelectat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ButtonAdaugaInLista, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(ButtonProceseaza)
                            .addComponent(ButtonRevocare))))
                .addGap(12, 12, 12))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @SuppressWarnings("static-access")
    private void list1ProduseExistenteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_list1ProduseExistenteActionPerformed
        // TODO add your handling code here:
         TextFieldItemSelectat.setText(list1ProduseExistente.getSelectedItem());
        


    }//GEN-LAST:event_list1ProduseExistenteActionPerformed

    @SuppressWarnings("static-access")
    private void ButtonAdaugaInListaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonAdaugaInListaActionPerformed
        // TODO add your handling code here:
        qty[j] = Integer.parseInt(SpinnerCantitate.getValue().toString());
        listProduseSelectate.add(list1ProduseExistente.getSelectedItem());
        transferedId[j] = idPizza[list1ProduseExistente.getSelectedIndex()];
        j++;
    }//GEN-LAST:event_ButtonAdaugaInListaActionPerformed

    private void ButtonRevocareActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonRevocareActionPerformed
        // TODO add your handling code here:
        dispose();
        setVisible(false);
    }//GEN-LAST:event_ButtonRevocareActionPerformed

    @SuppressWarnings({"static-access", "static-access", "static-access", "static-access"})
    private void ButtonProceseazaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonProceseazaActionPerformed
        try {
            // TODO add your handling code here:
            java.util.Date today = new java.util.Date();
            java.sql.Timestamp ts1 = new java.sql.Timestamp(today.getTime());
            Connection con = sqlh.conexiune;
            PreparedStatement ps = con.prepareStatement("INSERT INTO comanda(id_comanda,id_client,data,este_livrata,cine) VALUES (?,?,?,?,?)");
            ps.setString(1, null);
            ps.setInt(2, id_client);
            ps.setTimestamp(3,ts1);
            ps.setInt(4,0);
            ps.setString(5,"nimeni");
            int count = ps.executeUpdate();
            Statement stmnt = con.createStatement();
            stmnt.executeQuery("SELECT MAX(id_comanda) from comanda");
            ResultSet rs= stmnt.getResultSet();
            int k=0;
            while(rs.next())
            {
                k=rs.getInt("MAX(id_comanda)");
            }
            for(int ctr=0;ctr<j;ctr++)
            {
                PreparedStatement ps2= con.prepareStatement("INSERT INTO continut_comanda(id_cc,id_produs,qty,id_comanda) VALUES (?,?,?,?)");
                ps2.setString(1, null);
                ps2.setInt(2,transferedId[ctr]);
                ps2.setInt(3,qty[ctr]);
                ps2.setInt(4,k);
                int w = ps2.executeUpdate();
                }

            con.close();
            dispose();
            setVisible(false);
        } catch (SQLException ex) {
            Logger.getLogger(Clienti__existenti__onClick.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_ButtonProceseazaActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void mainACA(int argid) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Clienti__existenti__onClick().setVisible(true);
            }
        });
        id_client = argid;


    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonAdaugaInLista;
    private javax.swing.JButton ButtonProceseaza;
    private javax.swing.JButton ButtonRevocare;
    private javax.swing.JSpinner SpinnerCantitate;
    private javax.swing.JTextField TextFieldItemSelectat;
    private java.awt.List list1ProduseExistente;
    private java.awt.List listProduseSelectate;
    // End of variables declaration//GEN-END:variables

}
